import datetime
import math

from flask import Blueprint, render_template, request

from applications.common import curd
from applications.common.curd import model_to_dicts, enable_status, disable_status
from applications.common.helper import ModelFilter
from applications.common.utils.http import table_api, fail_api, success_api
from applications.common.utils.rights import authorize
from applications.common.utils.validate import xss_escape

from applications.models import PoliceModels, TrainData2,TrainData
from applications.schemas import PoliceOutSchema, TrainOutSchema,TrainDataSchema

trainreport_bp = Blueprint('admintrainreport', __name__, url_prefix='/admin/training_report')


# 管理员信息
@trainreport_bp.get('/')
# @authorize("xxxxxxxxxxxxxxxxxx", log=True)
def police_main():
    l1=[]
    l1.append(len(TrainData.query.filter_by(train_name="警员急症情景训练").all()))
    l1.append(len(TrainData.query.filter_by(train_name="服刑人员急症情景训练").all()))
    l1.append(len(TrainData.query.filter_by(train_name="打斗攻防情景训练").all()))
    l1.append(len(TrainData.query.filter_by(train_name="越狱攻防情景训练").all()))
    l1.append(len(TrainData.query.filter_by(train_name="袭警攻防情景训练").all()))

    return render_template('admin/atkdef_scene/scene_training_report.html', num = l1)


#   用户分页查询1
@trainreport_bp.get('/data')
# @authorize("xxxxxxxxxxxxxxxxxxx", log=True)
def data():
    # # 获取请求参数
    username = xss_escape(request.args.get('user_name', type=str))
    scenename = xss_escape(request.args.get('scene_name', type=str))
    trainname = xss_escape(request.args.get('train_name', type=str))
    # 查询参数构造
    mf = ModelFilter()
    if username:
        mf.contains(field_name="user_name", value=username)
    if scenename:
        mf.contains(field_name="scene_name", value=scenename)
    if trainname:
        mf.contains(field_name="train_name", value=trainname)
    # orm查询
    # 使用分页获取data需要.items
    # train = TrainData.query.filter(TrainData.train.train_type.contains(traindata)).layui_paginate()
    train = TrainData.query.filter(mf.get_filter(model=TrainData)).layui_paginate()
    count = train.total
    # 返回api
    return table_api(data=model_to_dicts(schema=TrainDataSchema, data=train.items), count=count)




#  用户详情
@trainreport_bp.get('/details/<int:id>')
# @authorize("xxxxxxxxxxxxxxxxxxx", log=True)
def police_type_details(id):
    train = curd.get_one_by_id(TrainData, id)
    end_time = train.end_time.timestamp()
    start_time = train.start_time.timestamp()
    duration = end_time - start_time
    time1 = start_time + duration / 6
    time2 = time1 + duration / 6
    time3 = time2 + duration / 6
    time4 = time3 + duration / 6
    time5 = time4 + duration / 6
    time6 = time5 + duration / 6

    t_duration = train.end_time - train.start_time



    return render_template('admin/atkdef_scene/scene_personal_report.html',
                           train=train,
                           t_min=math.floor(t_duration.seconds / 60),
                           t_sec=t_duration.seconds - math.floor(t_duration.seconds / 60) * 60,
                           t0=datetime.datetime.fromtimestamp(start_time).strftime('%H:%M:%S'),
                           t1=datetime.datetime.fromtimestamp(time1).strftime('%H:%M:%S'),
                           t2=datetime.datetime.fromtimestamp(time2).strftime('%H:%M:%S'),
                           t3=datetime.datetime.fromtimestamp(time3).strftime('%H:%M:%S'),
                           t4=datetime.datetime.fromtimestamp(time4).strftime('%H:%M:%S'),
                           t5=datetime.datetime.fromtimestamp(time5).strftime('%H:%M:%S'),
                           t6=datetime.datetime.fromtimestamp(time6).strftime('%H:%M:%S')

                           )


#  用户详情
@trainreport_bp.get('/psychological_details')
# @authorize("xxxxxxxxxxxxxxxxxxx", log=True)
def police_type_psychological_details():

    return render_template('admin/police/psychological_details.html')